package com.example.tbplus14.utils.poi;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.Data;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EasyExlTest {

    public static void main(String[] args) throws Exception{
        String filePath = "C:\\Users\\tb14plus\\Desktop\\压测" + File.separator + "emp.xlsx";

        List<ExcelEmpData> data = new ArrayList<>();
        int count = 100000;
        int sheetDataCount = 10000;

//        for (int i = 0; i < count; i++) {
//            ExcelEmpData empData = new ExcelEmpData();
//            empData.setName("员工" + i);
//            empData.setPassword("pw" + i);
//            empData.setBirthday(new Date());
//            empData.setSalary(10000.0 + i * 100.0);
//            data.add(empData);
//
//            // 每个sheet一百万数据
//            if (i > 0 && i % sheetDataCount == 0){
//                EasyExcel.write(filePath, ExcelEmpData.class).sheet("员工信息").doWrite(data);
//            }
//        }

        try (ExcelWriter excelWriter = EasyExcel.write(filePath, ExcelEmpData.class).build()) {
            // 去调用写入,这里我调用了五次，实际使用时根据数据库分页的总的页数来
            for (int i = 0; i < count; i++) {
                ExcelEmpData empData = new ExcelEmpData();
                empData.setName("员工" + i);
                empData.setPassword("pw" + i);
                empData.setBirthday(new Date());
                empData.setSalary(10000.0 + i * 100.0);
                data.add(empData);
                // 每个sheet一百万数据
                if (i > 0 && i % sheetDataCount == 0){
                    // 这里注意 如果同一个sheet只要创建一次
                    WriteSheet writeSheet = EasyExcel.writerSheet(String.format("sheet%s", i)).build();
                    excelWriter.write(data, writeSheet);
                }
            }
        }
    }

    @Data
    public static class ExcelEmpData{
        @ExcelProperty("姓名")
        private String name;
        @ExcelProperty("密码")
        private String password;
        @ExcelProperty("生日")
        private Date birthday;
        @ExcelProperty("银子")
        private double salary;
    }

}
